// -----------------------------------------------------------------------
//  <copyright file="UserController.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/10 12:30:25</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Identity.Application.Identity.Users.Commands;
using DaprPlus.Identity.Application.Identity.Users.Queries;


namespace DaprPlus.Identity.Api.Controllers
{
    [ApiResultWrapperFilter]
    public class UserController(IMediator mediator) : ApiControllerBase
    {
        [HttpGet("{id}")]
        [Produces<UserOutDto>]
        public async Task<ActionResult> Get(long id)
        {
            var query = new GetUserByIdQuery(id);
            var result = await mediator.Send(query);
            if (result == null)
            {
                return NotFound();
            }
            return Ok(result);
        }

        [HttpPost("read")]
        [Produces<PageResult<UserOutDto>>]
        public async Task<IActionResult> Read([FromBody] GetUserPageQuery query)
        {
            var page = await mediator.Send(query);
            return Ok(page);
        }

        [HttpPost]
        [Produces<ApiResult<UserOutDto>>]
        public async Task<ActionResult> Create([FromBody] CreateUserCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        [HttpPut]
        [Produces<ApiResult<UserOutDto>>]
        public async Task<ActionResult> Update([FromBody] UpdateUserCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        [HttpDelete]
        [Produces<ApiResult>]
        public async Task<ActionResult> Delete(long[] ids)
        {
            var result = await mediator.Send(new DeleteUserCommand(ids));
            return Ok(result);
        }

        [HttpPost("setpassword")]
        [Produces<ApiResult>]
        public async Task<ActionResult> SetPassword([FromBody] SetPasswordCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        [HttpPost("setrole")]
        [Produces<ApiResult>]
        public async Task<ActionResult> SetRole([FromBody] SetRoleCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }
    }
}
